//
//  OtherSetting.swift
//  GomroClient
//
//  Created by apple on 2021/6/13.
//  Copyright © 2021 cn.gomro.app. All rights reserved.
//

import Foundation
import Charts
import BusinessChat
import YYKit

/// 行情样式设置
@objc public class MarketSetting: NSObject {
    
    /// 游戏行情数据
    @objc public static let gameMarketList = "game_market_list"
    
    /// 累计收益率
    @objc public static let total_yield_key = "total_yield_key"
    
    /// 副图指标数据
    @objc public static var dicKpiData = [String:[Double]]()
    
    /// 主图指标数据
    @objc public static var dicKpiMainData = [String:[Double]]()
    
    /// 十字线颜色
    @objc public static let highlightColor = UIColor.lightGray
    
    /// 右边刻度颜色
    @objc public static let labelRightTextColor = UIColor.lightGray
    
    /// 右边刻度字体大小
    @objc public static let labelRightFont = UIFont.systemFont(ofSize: 10)
    
    /// 动画时间
    @objc public static let timeInterval = TimeInterval.init(1.5)
    
    /// 刻度颜色
    @objc public static let labelTextColor = NSUIColor.gray.withAlphaComponent(0.9)
    
    /// 刻度字体大小
    @objc public static let labelFont = UIFont.systemFont(ofSize: 10)
    
    /// 红涨
    @objc public static let red_color:UIColor = UIColor.colorFromHexInt(hex: 0xF5324D)
    
    /// 绿跌
    @objc public static let greent_color:UIColor = UIColor.colorFromHexInt(hex: 0x2DB476)
    
    /// dif 颜色
    @objc public static let dif_color:UIColor = UIColor.init(hexString: "#E6B970")!
    
    /// dea 颜色
    @objc public static let dea_color:UIColor = UIColor.init(hexString: "#4C6E9C")!
    
    /// macd 颜色
    @objc public static let kmacd_color:UIColor = UIColor.purple
    
    /// 其他
    @objc public static let kother_color:UIColor = UIColor.brown
    
    /// 比率
    @objc public static let right_ratio:Double = 1.5 / 288
    
    /// 副图指标
    @objc public static var arrMinorKPI = ["MACD","KDJ"]
    
    /// 主图指标
    @objc public static let arrMainKPI = ["MA","BOLL"]
    
    /// 副图当前指标索引
    @objc public static var minorCurrentIndex:Int = 0
    
    /// 主图当前指标索引
    @objc public static var mainCurrentIndex:Int = 0
    
    /// 行情分时值
    @objc public static let arrTimes = [5,15,30,60]
    
    /// 行情code
    @objc public static let arrCodes = ["SH000001","SH000300","SZ399001","SZ002371","HK09988","SZ399006","SZ000001","HK09618","HK09888","HK03690","HK01810"]
}


//MARK: -
/// 行情相关设置
@objc public extension MarketSetting {
    
    
    /// 设置缩放样式
    /// - Parameter chart: BarLineChartViewBase
    @objc static func setZoomStyleFor(Chart chart:BarLineChartViewBase){
        //缩放
        chart.scaleXEnabled = true
        chart.scaleYEnabled = false
        
        //自动缩放
        chart.autoScaleMinMaxEnabled = false
        chart.highlightPerTapEnabled = true
        
        //高亮拖拽
        chart.highlightPerDragEnabled = true
        
        //手势捏合
        chart.pinchZoomEnabled = false
        
        //开启拖拽
        chart.dragEnabled = true
        
        //0 1 惯性
        chart.dragDecelerationFrictionCoef = 0.95
        
        //最大缩放值 最小缩放值  y轴不缩放 minScl maxScl根据实际情况调整
        chart.setScaleMinima(5, scaleY: 1)
        
        //最大缩放级别
        chart.viewPortHandler.setMaximumScaleX(20)
        
        //是否显示图例
        chart.legend.enabled = false
        
        //禁止双击缩放 有需要可以设置为YES
        chart.doubleTapToZoomEnabled = false
        
        //最大显示数(两者搭配使用)
        chart.maxVisibleCount = 30
    }
    
    
    /// 设置空视图样式
    /// - Parameter chart: BarLineChartViewBase
    @objc public static func setEmptyStyleFor(Chart chart:BarLineChartViewBase) {
        //[S]暂无数据
        chart.noDataText = "暂无数据"
        chart.noDataTextAlignment = .center
        chart.noDataTextColor = UIColor.lightGray
        chart.noDataFont = UIFont.systemFont(ofSize: 15)
        //[E]
    }
    
    
    /// 设置右边纵轴刻度样式
    /// - Parameters:
    ///   - chart: BarLineChartViewBase
    ///   - c: 展示刻度数
    @objc public static func setRightCalibrationStyleFor(Chart chart:BarLineChartViewBase,
                                            andShowCount c:Int) {
        //[S]设置y轴相关参数 将坐标轴显示在右边
        let rightAxi:YAxis = chart.rightAxis
        rightAxi.enabled = true
        
        //保留两位小数
        rightAxi.decimals = 2
        
        //设置显示最大点数
        rightAxi.labelCount = c
        //强制label个数
        rightAxi.forceLabelsEnabled = false
        
        rightAxi.drawGridLinesEnabled = true
        rightAxi.drawAxisLineEnabled = false
        
        //内边框
        rightAxi.gridLineDashLengths = [5.0, 5.0]
        rightAxi.gridLineDashPhase = 0
        
        //label位置
        rightAxi.labelPosition = .outsideChart
        
        //文字颜色
        rightAxi.labelTextColor = MarketSetting.labelRightTextColor
        
        //文字字体
        rightAxi.labelFont = MarketSetting.labelRightFont
        //[E]
        
        //左边刻度
        chart.leftAxis.enabled = false
    }
    
    
    /// 设置左边纵轴刻度样式
    /// - Parameters:
    ///   - chart: BarLineChartViewBase
    ///   - c: 展示刻度数
    @objc public static func setLeftCalibrationStyleFor(Chart chart:BarLineChartViewBase,
                                           andShowCount c:Int) {
        //[S]设置y轴相关参数 将坐标轴显示在左边
        let leftAxi:YAxis = chart.leftAxis
        leftAxi.enabled = true
        
        //保留两位小数
        leftAxi.decimals = 2
        
        //设置显示最大点数
        leftAxi.labelCount = c
        //强制label个数
        leftAxi.forceLabelsEnabled = false
        
        leftAxi.drawGridLinesEnabled = true
        leftAxi.drawAxisLineEnabled = false
        
        //内边框
        leftAxi.gridLineDashLengths = [5.0, 5.0]
        leftAxi.gridLineDashPhase = 0
        
        //label位置
        leftAxi.labelPosition = .outsideChart
        
        //文字颜色
        leftAxi.labelTextColor = MarketSetting.labelRightTextColor
        
        //文字字体
        leftAxi.labelFont = MarketSetting.labelRightFont
        //[E]
        
        //右边刻度
        chart.rightAxis.enabled = false
    }
    
    
    /// 设置底部横轴样式
    /// - Parameters:
    ///   - chart: BarLineChartViewBase
    ///   - show: true 显示刻度 防止不显示
    ///   - c: 显示刻度条数
    @objc public static func setBottomCalibrationStyleFor(Chart chart:BarLineChartViewBase,
                                             andIsShow show:Bool,
                                             withShowCount c:Int) {
        //设置X轴相关参数
        chart.xAxis.enabled = show
        chart.xAxis.labelPosition = .bottom
        chart.xAxis.labelCount = c
        chart.xAxis.drawGridLinesEnabled = false
        chart.xAxis.drawAxisLineEnabled = true
        
        //避免文字显示不全 这个属性很重要
        chart.xAxis.avoidFirstLastClippingEnabled = true
        
        //第一个和最后一个图会显示一半(显示不全)
        chart.xAxis.spaceMin = 0.5
        chart.xAxis.spaceMax = 0.5
        
        //强制label个数
        chart.xAxis.forceLabelsEnabled = false
        
        //设置重复不显示
        chart.xAxis.granularityEnabled = true
        
        //间距
        chart.xAxis.granularity = 1
    }
    
    
    /// 设置背景、边框等样式
    /// - Parameters:
    ///   - chart: BarLineChartViewBase
    ///   - sgbg: true 显示背景
    ///   - bgc: 背景色
    ///   - sb: true 显示边框
    ///   - bdc: 边框背景
    ///   - edg: UIEdgeInsets 偏移(防止靠边没显示全)
    @objc public static func setGradStyleFor(Chart chart:BarLineChartViewBase,
                                andShowGridBackground sgbg:Bool = false,
                                withBGColor bgc:UIColor = UIColor.clear,
                                andShowBorders sb:Bool = false,
                                andBorderColor bdc:UIColor = UIColor.clear,
                                withEdgeInsets edg:UIEdgeInsets = UIEdgeInsets.init(top: 10, left: 10, bottom: 10, right: 10),
                                andDelegate _delegate:ChartViewDelegate? = nil) {
        
        //是否显示气泡
        chart.drawMarkers = false
        chart.delegate = _delegate
        
        //画板以及边框颜色
        chart.gridBackgroundColor = bgc
        chart.borderColor = bdc
        
        //根据需要显示或隐藏边框以及画板
        chart.drawGridBackgroundEnabled = sgbg
        chart.drawBordersEnabled = sb
        
        chart.backgroundColor = bgc
        
        //设置偏移
        chart.setExtraOffsets(left: edg.left,
                              top: edg.top,
                              right: edg.right,
                              bottom: edg.bottom)
        
        //描述文字
        chart.chartDescription?.enabled = false
    }
    
    
    /// 设置股票数据源
    /// - Parameter yv: [ChartDataEntry]
    /// - Returns: CandleChartDataSet
    @objc public static func setStockDataSetFor(Values yv:[ChartDataEntry]) -> CandleChartDataSet {
        let dataSet = CandleChartDataSet.init(entries: yv, label: nil)
        
        //轴线方向
        dataSet.axisDependency = .right
        
        //不在面板上直接显示数值
        dataSet.drawValuesEnabled = false
        
        //true 蜡烛图 false 美国线
        dataSet.showCandleBar = true
        
        //k线柱间隙
        dataSet.barSpace = 0.1
        
        //这是用于显示最高最低值区间的立线
        //dataSet.shadowColor = UIColor.black
        
        //立线的宽度
        dataSet.shadowWidth = 0.7
        dataSet.shadowColorSameAsCandle = true
        
        // close >= open 红涨
        dataSet.increasingColor = red_color
        dataSet.neutralColor = red_color
        // 内部是否充满颜色
        dataSet.increasingFilled = true
        
        // open > close 绿跌
        dataSet.decreasingColor = greent_color
        // 内部是否充满颜色
        dataSet.decreasingFilled = true
        
        //十字线
        dataSet.drawHorizontalHighlightIndicatorEnabled = false
        dataSet.drawVerticalHighlightIndicatorEnabled = false
        dataSet.highlightEnabled = true
        
        return dataSet
    }
    
    
    /// 设置LineChartDataSet
    /// - Parameters:
    ///   - yv: [ChartDataEntry]
    ///   - c: 线条颜色
    ///   - n: 线条名称
    /// - Returns: LineChartDataSet
    @objc public static func setLineDataFor(Values yv:[ChartDataEntry],
                               andColor c:UIColor,
                               withName n:String) -> LineChartDataSet {
        
        //创建LineChartDataSet对象
        let picketageSet = LineChartDataSet.init(entries: yv, label: n)
        
        //不在面板上直接显示数值
        picketageSet.drawValuesEnabled = false
        
        //十字线
        picketageSet.drawHorizontalHighlightIndicatorEnabled = false
        picketageSet.drawVerticalHighlightIndicatorEnabled = false
        picketageSet.highlightEnabled = false
        
        //十字线高亮颜色
        picketageSet.highlightColor = highlightColor
        
        //线条颜色
        picketageSet.setColor(c)
        
        //没有圆圈
        picketageSet.circleRadius = 0.0
        picketageSet.circleHoleRadius = 0.0
        
        return picketageSet
    }
    
    
    /// 设置柱状图数据
    /// - Parameters:
    ///   - yv: [ChartDataEntry]
    ///   - n: 名称
    /// - Returns: BarChartDataSet
    @objc public static func setBarCharSetFor(Values yv:[ChartDataEntry],
                                 withName n:String) -> BarChartDataSet {
        
        let barChartDataSet = MyBarChartDataSet.init(entries: yv, label: n)
        
        barChartDataSet.axisDependency = .left
        barChartDataSet.setColors(red_color,greent_color)
        
        //不在面板上直接显示数值
        barChartDataSet.drawValuesEnabled = false
        barChartDataSet.drawIconsEnabled = false
        
        return barChartDataSet
    }
    
    /// 设置柱状图数据(自定义颜色)
    /// - Parameters:
    ///   - yv: [ChartDataEntry]
    ///   - c: [UIColor]
    ///   - n: 名称
    /// - Returns: BarChartDataSet
    @objc public static func setBarCharSetFor(Values yv:[ChartDataEntry],
                                 andColors c:[UIColor],
                                 withName n:String) -> BarChartDataSet {
        
        let barChartDataSet = MyBarChartColorsDataSet.init(entries: yv,
                                                           andColors: c,
                                                           label: n)
        
        barChartDataSet.axisDependency = .left
        barChartDataSet.colors = c
        
        //不在面板上直接显示数值
        barChartDataSet.drawValuesEnabled = false
        barChartDataSet.drawIconsEnabled = false
        
        return barChartDataSet
    }
    
    
    /// 设置柱状图
    /// - Parameter yv: [ChartDataEntry]
    /// - Returns: BarChartData
    @objc public static func setBarDataSetsFor(Values yv:[BarChartDataEntry]) -> [BarChartDataSet] {
        
        var arrTemp = [BarChartDataSet]()
        
        for item in yv {
            //创建BarChartDataSet对象
            let picketageSet = BarChartDataSet.init(entries: [item], label: nil)
            
            //不在面板上直接显示数值
            picketageSet.drawValuesEnabled = false
            
            //是否显示十字线
            picketageSet.highlightEnabled = true
            
            arrTemp.append(picketageSet)
        }
        
        return arrTemp
    }
}
